package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.system.dao.SysTenantDynamicDataDao;
import com.ccp.dev.system.model.SysTenantDynamicData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.zip.DataFormatException;

/**
 * 租户数据关联
 * @author y
 */
@Service
public class SysTenantDynamicDataService extends BaseService<SysTenantDynamicData> {

	@Resource
    SysTenantDynamicDataDao sysTenantDynamicDataDao;
    /**
     * 根据数据id删除数据
     * @param dataId 数据id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByDataId(String dataId){
        sysTenantDynamicDataDao.delByDataId(dataId);
    }
    /**
     * 根据租户id删除数据
     * @param tenantId 租户id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByTenantId(String tenantId){
        sysTenantDynamicDataDao.delByTenantId(tenantId);
    }

    /**
     * 根据数据id获取租户id
     * @param dataId 数据id
     * @return string 租户id
     */
    public String getTenantIdByDataId(String dataId){
        return sysTenantDynamicDataDao.getTenantIdByDataId(dataId);
    }

    /**
     *
     * @param dataId  数据id
     * @param tenantId 租户id
     * @param clazz 类名称
     */
    public void addSysTenantDynamicData(String dataId, String tenantId,  Class<? extends BaseModel> clazz) {
        SysTenantDynamicData dataMapping = new SysTenantDynamicData();
        dataMapping.setTenantDataId(UUIDUtils.getUUIDFor32());
        dataMapping.setDataId(dataId);
        dataMapping.setTenantId(tenantId);
        dataMapping.setClassName(clazz.getSimpleName());
        this.add(dataMapping);
    }

    /**
     * 根据租户id查询
     * @param tenantId 租户id
     * @return 返回SysTenantDynamicData对象集合 该对象是角色与租户的关联表
     */
    public List<SysTenantDynamicData> queryByTenantId(String tenantId) {
        return sysTenantDynamicDataDao.queryByTenantId(tenantId);
    }
}
